package com.mytijian.openapi.adapter.alihalt.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.mytijian.code.dto.QueryItemIdByCodeResult;
import com.mytijian.code.param.alihalt.QueryItemIdByCodeParam;
import com.mytijian.code.service.MerchantService;
import com.mytijian.openapi.adapter.alihalt.consts.AlihaltConsts;
import com.mytijian.openapi.adapter.alihalt.exception.AlihaltException;
import com.mytijian.openapi.adapter.alihalt.param.AlihaltMealInfoParam;
import com.mytijian.openapi.adapter.alihalt.result.AlihaltMealInfo;
import com.mytijian.openapi.adapter.alihalt.result.AlihaltMealInfoResult;
import com.mytijian.openapi.adapter.alihalt.service.AlihaltMealInfoService;
import com.mytijian.openapi.core.client.IOpenAPIClient;
import com.mytijian.openapi.core.client.result.ExamItemInfo;
import com.mytijian.openapi.core.client.result.MealInfo;
import com.mytijian.openapi.core.model.AppConfig;
import com.mytijian.openapi.core.service.OpenApiAppConfigService;

@Service
public class AlihaltMealInfoServiceImpl implements AlihaltMealInfoService {

	private Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	private OpenApiAppConfigService openApiService;

	@Autowired
	private MerchantService merchantService;

	@Autowired
	private IOpenAPIClient client;

	@Override
	public AlihaltMealInfoResult getMealInfo(AlihaltMealInfoParam param) {

		log.info("[阿里健康] [获取套餐详情] 传入参数: {}", JSON.toJSONString(param));

		String tmallProductId = getTmallProductId(param);

		// 开放平台提供使用天猫商品id获取套餐的接口

		AppConfig config = openApiService.getConfig(AlihaltConsts.AppName);

		MealInfo meal = client.getMealByTmallProductId(config, tmallProductId);

		if (!verifyMeal(meal)) {
			log.error("*** [阿里健康] [套餐查询] OpenAPI 调用失败。 接口返回参数: {}", JSON.toJSONString(meal));
			throw new AlihaltException("500", "套餐查询错误");
		}

		AlihaltMealInfoResult result = new AlihaltMealInfoResult();
		result.setResponseCode("200");

		result.setHospitalCode(String.valueOf(meal.getHospitalId()));
		result.setHospitalName(meal.getHospitalName());
		result.setPackageCode(String.valueOf(meal.getMealId()));
		result.setPackageName(meal.getName());
		result.setPackageGender(meal.getGender());

		List<AlihaltMealInfo> aliMealList = new ArrayList<>();

		for (ExamItemInfo info : meal.getExamItems()) {
			AlihaltMealInfo aliMealInfo = new AlihaltMealInfo();
			aliMealInfo.setCheckItemName(info.getName());
			aliMealInfo.setDescription(info.getDescription());
			aliMealList.add(aliMealInfo);
		}

		result.setResult(aliMealList);

		log.info("[阿里健康] [套餐查询] 执行成功。");
		return result;

	}

	private boolean verifyMeal(MealInfo meal) {
		if (meal == null) {
			return false;
		}

		if (StringUtils.isNotEmpty(meal.getErrCode())) {
			return false;
		}

		if (meal.getMealId() == null) {
			return false;
		}

		return true;
	}

	private String getTmallProductId(AlihaltMealInfoParam param) {
		String mcode = param.getServiceNumber();
		QueryItemIdByCodeParam qp = new QueryItemIdByCodeParam();
		qp.setCode(mcode);
		QueryItemIdByCodeResult tmprod = null;

		try {
			tmprod = merchantService.queryItemIdByCode(qp);
		} catch (RuntimeException e) {
			throw new AlihaltException("400", e.getMessage());
		}

		if (tmprod == null || StringUtils.isEmpty(tmprod.getItemId())) {
			throw new AlihaltException("400", "电子码无效");
		}

		String tmallProductId = tmprod.getItemId();
		return tmallProductId;
	}

}
